package com.zxy.demo.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zxy.demo.common.utils.SpringContextUtils;
import com.zxy.demo.test.TestSpringInterface;
import com.zxy.demo.validate.ValidatorEnum;
import com.zxy.demo.validate.annotation.ValidateParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags={"测试Controller"})
@Controller
@RequestMapping("/test")
public class TestController {
	
	@Autowired
	private List<TestSpringInterface> testSpringInterfaceList;
	
	@GetMapping("/hello")
	public String hello() {
		int a = 1/0;
		return "a";
	}
	
	@GetMapping("/hello2")
	@ResponseBody
	public Map<String,Object> hello2() {
		Map<String,Object> map = null;
		map.put("aa", 10);
		return map;
	}
	
	@PostMapping("/testMyValdate")
	@ApiOperation (value = "测试自定义注解进行参数校验", httpMethod = "POST", response = String.class)
	@ResponseBody
	public String testMyValdate(@ValidateParam(value = ValidatorEnum.NOT_BLANK,name="参数a") String a,
			@ValidateParam(value = ValidatorEnum.INT,name="参数b") Integer b) {
		return a + b;
	}
	
	
	@GetMapping("/testSpringOrder")
	@ApiOperation (value = "测试Spring的Order注解", httpMethod = "GET", response = String.class)
	@ResponseBody
	public List<String> testSpringOrder() {
		List<String> result = new ArrayList<>();
		// 用容器获取时，排序是不生效的，这里是因为我自己写代码给排序了
		List<TestSpringInterface> list = SpringContextUtils.getBeansWithSort(TestSpringInterface.class);
		for (TestSpringInterface testSpringInterface : list) {
			result.add(testSpringInterface.sayHi());
		}
		return result;
	}
	
	@GetMapping("/testSpringOrder2")
	@ApiOperation (value = "测试Spring的Order注解2", httpMethod = "GET", response = String.class)
	@ResponseBody
	public List<String> testSpringOrde2() {
		List<String> result = new ArrayList<>();
		// 用@Autowired注解注入则Order生效
		for (TestSpringInterface testSpringInterface : testSpringInterfaceList) {
			result.add(testSpringInterface.sayHi());
		}
		return result;
	}
	
}
